# coding=utf-8
import datetime

try:
    from mod.client.ui.controls.baseUIControl import *
    from typing import *
except ImportError:
    BaseUIControl = None
    ImageUIControl = None
    LabelUIControl = None


class ControlState(object):
    Finish = 0
    Run = 1
    Suspend = 2


class Base(object):
    state = ControlState.Finish
    suspendTimePeriod = 0.0
    runTime = datetime.datetime.now()
    suspendTimePoint = runTime

    def __init__(self):
        self.runTime = datetime.datetime.now()
        self.suspendTimePoint = self.runTime
        self.suspendTimePeriod = 0.0

    def Refresh(self):
        """
        刷新,开始执行移动任务时需要刷新
        :return:已执行任务的时间
        """
        if not self.state == ControlState.Run:
            return
        now = (datetime.datetime.now() - self.runTime).total_seconds() - self.suspendTimePeriod
        return now

    def GetWorkTime(self):
        """
        获取执行了多少时间
        """
        return (datetime.datetime.now() - self.runTime).total_seconds() - self.suspendTimePeriod

    def Suspend(self):
        """暂停任务.与完成不同的是,下一次开始时,继续执行暂停时的状态"""
        self.state = ControlState.Suspend
        self.suspendTimePoint = datetime.datetime.now()

    def Reset(self):
        """重置已运作时间"""
        self.runTime = datetime.datetime.now()

    def GetState(self):
        # type:()->str
        """
        获取状态
        """
        return self.state

    def Start(self):
        """开始任务"""
        if self.state == ControlState.Finish:
            self.runTime = datetime.datetime.now()
            self.state = ControlState.Run
        elif self.state == ControlState.Suspend:
            self.suspendTimePeriod += (datetime.datetime.now() - self.suspendTimePoint).total_seconds()
            self.state = ControlState.Run

    def Finish(self):
        """完成任务"""
        self.runTime = datetime.datetime.now()
        self.suspendTimePoint = self.runTime
        self.suspendTimePeriod = 0.0
        self.state = ControlState.Finish
        # 初始化
